package com.college.eduService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.college.commonutil.result.Result;
import com.college.eduService.entity.EduCourse;
import com.college.eduService.entity.frontvo.CourseWebVo;
import com.college.eduService.entity.vo.CourseInfoVo;
import com.college.eduService.entity.vo.CoursePublishVo;
import com.college.eduService.entity.query.CourseQuery;
import com.college.eduService.entity.vo.EduCourseVo;
import com.college.eduService.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(description="课程管理")
@RestController
@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    EduCourseService courseService;
    //分页查询课程
    @PostMapping("pageQuery/{current}/{limit}")
    public Result pageQuery(@PathVariable Integer current, @PathVariable Integer limit,
                            @RequestBody CourseQuery courseQuery) {
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.eq("status","Draft");
        int count=courseService.count(wrapper);
        Page<EduCourse> page =  courseService.pageQuery(current,limit,courseQuery);
        return Result.ok().data("count",count).data("total",page.getTotal()).data("list",page.getRecords());
    }
    //删除课程
    @DeleteMapping("removeCourse/{id}")
    public Result removeCourse(@PathVariable String id){
        courseService.removeCourse(id);
        return Result.ok();
    }
    //添加课程信息
    @PostMapping("savecourse")
    public Result saveCourse(@RequestBody CourseInfoVo courseInfoVo){
        String id=courseService.saveCourse(courseInfoVo);
        //返回课程id
        return Result.ok().data("courseid",id);
    }
    //根据id查询课程信息
    @GetMapping("getcourseId/{courseId}")
    public Result getcourseId(@PathVariable String courseId){
        CourseInfoVo courseInfoVo=courseService.getCourseById(courseId);
        return Result.ok().data("courseInfoVo",courseInfoVo);
    }
    //修改课程信息
    @PostMapping("updateCourse")
    public Result updateCourse(@RequestBody CourseInfoVo courseInfoVo){
        courseService.UpdateCourse(courseInfoVo);
        return Result.ok();
    }
    //最终提交根据id信息显示
    @ApiOperation(value = "根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public Result getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return Result.ok().data("publishCourse",coursePublishVo);
    }
    //修改status字段为发布
    @PostMapping("updateCourseStatus/{courseId}")
    public Result updateCourseStatus(@PathVariable String courseId){
        EduCourse course=new EduCourse();
        course.setId(courseId);
        course.setStatus("Normal");//设置课程为发布状态
        courseService.updateById(course);
        return Result.ok();
    }
    //根据课程id，查询课程信息【订单】
    @PostMapping("/getCourseInfoByIdOrder/{courseId}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable String courseId){
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(courseId);

        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(courseInfo,eduCourseVo);

        return eduCourseVo;

    }
    //播放历史课程集合
    @GetMapping("getCourseInfoHistory/{courseId}")
    public List<EduCourse> getCourseInfoHistory(@PathVariable String courseId) {
        List<EduCourse> courseList=courseService.getCourseHistory(courseId);
        return courseList;
    }
    //查询
}
